package GUI;
/**
 * student type main menu, this is a specific frame for the Student type user
 * in this frame there is a menu with all of the student functionalities 
 */

import java.awt.Dimension;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;

import ClientSide.ChatClient;
import GUI.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class StudentMainMenu extends JFrame implements Serializable{
	
	private JMenuBar menuBar=null;
	private JMenu mAcademicActivity=null;
	private JMenu mAcademicActivity_1;
	private JMenuItem mntmSubmitSolution=null; 
	private JMenuItem mntmViewCheckedAssignments=null;
	private JMenu mPersonalData=null;
	private JMenuItem mntmViewPersonalData=null;
	private JMenuItem logout=null;
	private JMenuItem mntmViewNewAssigments=null;
	public static Dimension defaultDim=null;
	public Login myLoginFrame = null;
	public ViewPersonalData viewPD=null;
	
	public UserStudent user=null;// my add
    public boolean wait=false;					 //Flag for letting the ViewNewAssignment Tread to work prop
	private JFileChooser fc;	                  /// for choosing a file menu
	public static ChatClient client=null;		 /// For sending a file to server
	
	public String taskNum = null;				//Saving the details...	
	public String deadLine = null;				//for the ...
	public byte [] myByteArr=null;				///ViewNewAssign Table
	
	private AAViewNewAssignmentScreen newAssignmentScr=null;
	private ViewCheckedAssignmentScreen checkedAssignmentScr = null;
	private JMenuBar createMenuBar()
	
	{
		if (menuBar==null)
			
		{
			 menuBar = new JMenuBar();
			 mAcademicActivity_1 = new JMenu("Academic activity");
			 fc= new JFileChooser();
			 mntmSubmitSolution = new JMenuItem("Submit solution");
			 mntmSubmitSolution.addActionListener(new java.awt.event.ActionListener()
				{	
					public void actionPerformed(java.awt.event.ActionEvent e)
				
					{
						//Sending file to server:
						//1 getting the file from client:
						int returnVal = fc.showOpenDialog(StudentMainMenu.this);
						if (returnVal == JFileChooser.APPROVE_OPTION){
							//Geting the File
							File sCompliteAssign = fc.getSelectedFile();
							String classID= sCompliteAssign.getName().substring(0,5);
							String taskNum=sCompliteAssign.getName().substring(5,6);
							String userID = myLoginFrame.userID;
							String msg="stusendtask "+userID+" "+classID+" "+taskNum+" "+sCompliteAssign.getAbsolutePath();
							System.out.println("user id  is : "+userID+"\nclassID is : "+classID+"\ntaskNum is : "+taskNum+"\nfilename Is : "+sCompliteAssign.getName());
	//							sCompliteAssign = new CMyFile("sts"+sCompliteAssign.getName());
//							System.out.println("Stu Menu : "+sCompliteAssign.getTotalSpace()+"\n" + sCompliteAssign.getName()+"\nthe file lenght : " + (int)sCompliteAssign.length()+"\n"+sCompliteAssign.getAbsolutePath());
							
//							try {
//								stuSendTask = new mysqlConnection();
//							} catch (Exception e1) {
//								System.out.println("Error in StudentMain Menu making sqlCON");
//								e1.printStackTrace();
//							}
							myLoginFrame.client.handleMessageFromClientUI(msg);
						//	myLoginFrame.client.handleFileFromUI(sCompliteAssign);
						//	stuSendTask.sendFileToDB("sts"+sCompliteAssign.getName(),sCompliteAssign.getPath());
//							myLoginFrame.client.handleFileFromUI(stuSendFile.sendFileFromClientToServer("sts"+sCompliteAssign.getName(),sCompliteAssign.getPath()));							
						//	myLoginFrame.client.handleFileFromUI(stuSendTask.sendFileFromClientToServer("sts"+sCompliteAssign.getName(),sCompliteAssign.getPath()));
						}
						
						//TODO what happens when a student clicks "mntmSubmitSolution" 
				}
			});
			 mPersonalData = new JMenu("Academic data");
			 
			  mntmViewPersonalData = new JMenuItem("View personal data");
			  mntmViewPersonalData.addActionListener(new java.awt.event.ActionListener()
				{	
					public void actionPerformed(java.awt.event.ActionEvent e)
				
					{
						System.out.println("mntmViewPersonalData");
						viewPD = null;
						viewPD=getViewPersonalDataPanel();
						viewPD.myLoginFrame=myLoginFrame;
						
					
						
						//viewPD.myLoginFrame.client.handleMessageFromClientUI("getYears"+" "+myLoginFrame.userID);
						//while(myLoginFrame.EYear==null);
						//while(myLoginFrame.SYear==null);
					//	viewPD.getComboBoxSelYear();
						
						
						;
					//	viewPD.add(viewPD.getComboBoxSelYear(user.StudentData.Years));
					//	viewPD.add(viewPD.getComboBoxSelSemester(user.StudentData.Semesters));
					//	viewPD.add(viewPD.getComboBoxSelCourse(user.StudentData.ListOfCourses));
						
						setContentPane(viewPD);
						
						
						
						
						//TODO what happends when a parent clicks "mntmSubmitSolution" 
						
				}
			});
			  logout = new JMenuItem("Logout");
			  logout.addActionListener(new java.awt.event.ActionListener()
				{	
					public void actionPerformed(java.awt.event.ActionEvent e)
				
					{
						System.out.println("logout");
						if(myLoginFrame.LoginResult.equalsIgnoreCase("OK"))
						{
							myLoginFrame.client.handleMessageFromClientUI("logout "+myLoginFrame.UserName.getText().toString());
						}
						myLoginFrame.UserName.setText("");
						myLoginFrame.Password.setText("");
						myLoginFrame.setContentPane(myLoginFrame.getFirstPanel());
						myLoginFrame.setVisible(true);
						
						myLoginFrame.LoginResult=null;
						myLoginFrame.UserType=null;
						
						setVisible(false);
											
						// what happends when a parent clicks "mlogout" 
						
				}
			});
			  
			  
			  mntmViewNewAssigments = new JMenuItem("View new assigments");
			  mntmViewNewAssigments.addActionListener(new ActionListener() {
			  	public void actionPerformed(ActionEvent e) {
			  	//////////////////
			  		newAssignmentScr = null;
			  		newAssignmentScr = getNewAssignmentScr();
			  		newAssignmentScr.setSize(getMaximumSize());
			  		setContentPane(newAssignmentScr);
			  		
			  		
			  		
			  	}
			  });
		}//end if
			
		return menuBar;
			
			
	}
	private void initialize()
	{
		this.setSize(defaultDim=new Dimension(600,600));
		this.setTitle("Student main menu");
		this.setJMenuBar(createMenuBar());
		menuBar.add(mAcademicActivity_1);
		mAcademicActivity_1.add(mntmSubmitSolution);
		
		
		mAcademicActivity_1.add(mntmViewNewAssigments);
		menuBar.add(mPersonalData);
		mPersonalData.add(mntmViewPersonalData);
		menuBar.add(logout);
		//this.viewPD= new ViewPersonalData();
	//	 getViewPersonalDataPanel();
		//viewPD.myLoginFrame=this.myLoginFrame;
		//System.out.println("\n " +viewPD.myLoginFrame );
		//this.myLoginFrame.client.handleMessageFromClientUI("getYears"+" "+myLoginFrame.userID);
	
		//viewPD.myLoginFrame=this;
		
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	
		
		
	}
	
	
	
	
	
	public StudentMainMenu() {
		
		super();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				try {
					myLoginFrame.client.closeConnection();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 System.out.println("\n BYE!!");
		    
				
			}
		});
		initialize();
		
		
		
	}
	
	public ViewPersonalData getViewPersonalDataPanel()
	{
		if(viewPD==null)
		{
			viewPD = new ViewPersonalData(this);
		//	viewPD.setLayout(null);
		//	viewPD.setSize(defaultDim);
			viewPD.myLoginFrame=this.myLoginFrame;
			viewPD.user=this.user;
			viewPD.stmn=this;
			
		}
		return viewPD;
		
	}
	

	
	public AAViewNewAssignmentScreen getNewAssignmentScr()
	{
		if(newAssignmentScr==null)
		{
			newAssignmentScr = new AAViewNewAssignmentScreen(this);
		
			newAssignmentScr.myLoginFrame=this.myLoginFrame;
			newAssignmentScr.user=this.user;
			newAssignmentScr.stmn=this;
		
			
			
			
			
		}
		return newAssignmentScr;
	
	
	}
	
	
	
	public ViewCheckedAssignmentScreen getCheckedAssignmentScr()
	{
		if(checkedAssignmentScr==null)
		{
			checkedAssignmentScr = new ViewCheckedAssignmentScreen(this);
		
			checkedAssignmentScr.myLoginFrame=this.myLoginFrame;
			checkedAssignmentScr.user=this.user;
			checkedAssignmentScr.stmn=this;
			checkedAssignmentScr.setSize(getMaximumSize());
			
		}
		return checkedAssignmentScr;
	
	
	}
	public void setTaskNume(String tasknum){
		
		System.out.println("taskNum is saved USERTSTU: \n"+tasknum);
		this.taskNum=tasknum;
    	
    }
    public String getTaskNum(){
    	
    	return (taskNum);
    }
	public void setDeadLine(String deadLine){
    	
		this.deadLine=deadLine;
    	System.out.println("deadline is saved USERTSTU :\n "+deadLine);
    	 wait=true;
    }
    public String getDeadLine(){
    	
    	return (deadLine);
    }
	public void setMyByteArr(byte [] myByteArr) {
		this.myByteArr=myByteArr;
		
	}   
	public byte[] getMyByteArr(){
		
		return(myByteArr);
	}
}
